package navkakri;
import java.awt.*;
import java.applet.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JOptionPane;

public class ViewController extends Applet implements MouseListener,MouseMotionListener {

    public void init() {
        setBackground(Color.black);
        addMouseListener(this);
        addMouseMotionListener(this);
        // TODO start asynchronous download of heavy resources
    }

      public void start() {

}

public void mouseClicked(MouseEvent me) {
// save coordinates
Model.mouseX = me.getX();
Model.mouseY = me.getY();
if(Model.PutKukriMode==true){
    int click=Model.GetRectNo(Model.mouseX,Model.mouseY); // reModel.turns -2 if clicked outside all rectangles
    // reModel.turns -1 if rectangle is already filled
    //reModel.turns number of the empty rectangle on which we clicked
   // JOptionPane.showMessageDialog(null, "Rect No="+click);

    if(click==-1){
        JOptionPane.showMessageDialog(null, "Sorry U clicked outside all Rectangles");
    }

    else{
            if(Model.spaces[click]!=0){// if kukri is already placed at that place
                JOptionPane.showMessageDialog(null, "Sorry The places has been already filled");
            }
            else{
                Model.spaces[click]=Model.turn;
                if(Model.turn==1){
                    Model.kc1--;
                    Model.m3="Kukries in hand :="+Model.kc1;
                    Model.m6="Kukries in hand :="+Model.kc2;
                }
                else{
                    Model.kc2--;
                    Model.m3="Kukries in hand :="+Model.kc1;
                    Model.m6="Kukries in hand :="+Model.kc2;
                }
                if(Model.kc1<=0 && Model.kc2<=0){
                    Model.PutKukriMode=false;
                    Model.PickppKukMode=false;
                    Model.NormalMode=true;
                }
                int line=Model.CheckLine();
                if(line==1){
                    Model.PutKukriMode=false;
                    Model.NormalMode=false;
                    Model.PickppKukMode=true;
                    Model.m8="Congratulations Now Player "+Model.turn+" can Pick one Kukri of  Player "+(Model.turn==1?2:1)+"\n";
                    Model.m9=" Select Any One Opponent Kukri";
                    //m3="";
                }
                else{
                    if(Model.turn==1){
                    Model.m7="Player 2 Kukris left-->"+Model.p2KukLeft;
                    Model.m4="Player 1 Kukris left-->"+Model.p1KukLeft;
                    }
                    else{
                    Model.m7="Player 2 Kukris left-->"+Model.p2KukLeft;
                    Model.m4="Player 1 Kukris left-->"+Model.p1KukLeft;
                    }

                Model.changeTurn();
                }

                repaint();
        } // else ends
    }

} // if ends
else if (Model.PickppKukMode==true){
         int click=Model.GetRectNo(Model.mouseX,Model.mouseY);
        if(click!=-1){
            if(Model.spaces[click]==Model.turn || Model.spaces[click]==0){
                JOptionPane.showMessageDialog(null, "U have to pick Opponent's kukri Not Urs or empty space");
            }
            else{
                // check if kukri is a part of line then it cannot be moved
                int PArt=Model.CheckIfKukriIsPArtOfLine(click);
                 if(PArt!=1){
                    if(Model.turn==1){
                        Model.p2KukLeft--;
                        if(Model.p2KukLeft<=2){
                            Model.m10="Player 1 wins";
                        }
                        else{
                        Model.m7="Player 2 Kukris left-->"+Model.p2KukLeft;
                        Model.m4="Player 1 Kukris left-->"+Model.p1KukLeft;
                        }
                    }
                    else{
                        Model.p1KukLeft--;
                        if(Model.p1KukLeft<=0){
                            Model.m10="Player 2 wins";
                        }
                        else{
                        Model.m4="Player 1 Kukris left-->"+Model.p1KukLeft;
                        Model.m7="Player 2 Kukris left-->"+Model.p2KukLeft;
                        }
                    }
                    Model.spaces[click]=0;
                    Model.changeTurn();
                    Model.PickppKukMode=false;
                    if(Model.kc1<=0 && Model.kc2<=0){
                    Model.NormalMode=true;
                    //System.out.println("Normal  Mode="+NormalMode);
                    Model.PutKukriMode=false;
                    }
                    else{
                    Model.PutKukriMode=true;
                    Model.NormalMode=false;
                    }
                    Model.m8="";
                    Model.m9="";
                repaint();
                }
                 else{
                    JOptionPane.showMessageDialog(null, "U cannot Pick Up Kukri which is a part of LINE");
                 }
            }
        }

        else
        {
            JOptionPane.showMessageDialog(null, "Sorry U clicked at wrong place");
        }

}
else if(Model.NormalMode==true){
        if(Model.nmodeflag==0){ // if kukri is not selected
            int click=Model.GetRectNo(Model.mouseX,Model.mouseY);
            if(click!=-1){
                if(Model.spaces[click]==Model.turn){
                    int canMove=Model.CheckIfKukriCanMove(click);
                    if(canMove==1){
                        // if this kukri is part of line
                        // remove if from already made
                        int PartOfLine=Model.CheckIfKukriIsPArtOfLine(click);
                        if(PartOfLine==1){
                        Model.RemoveFromAlreadyMade(Model.turn,click);
                        JOptionPane.showMessageDialog(null,"Remove Already Made Model.turn="+Model.turn+" and Rect No"+click);
                        }
                        Model.outlined=click;
                        Model.nmodeflag=1;
                    }
                    else{
                        JOptionPane.showMessageDialog(null,"Sorry That kukri cannot move");
                    }
                }
                else{
                     JOptionPane.showMessageDialog(null,"Sorry That is not ur kukri");
                }
            }
            else{
                JOptionPane.showMessageDialog(null,"Sorry U clicked at Wrong Place");
            }
        }// if Kukri is selected
        else{
             int click=Model.GetRectNo(Model.mouseX,Model.mouseY);
             if(click!=-1){
                if(Model.spaces[click]==0){
                    if(Model.possibilities[Model.outlined][0]==click || Model.possibilities[Model.outlined][1]==click || Model.possibilities[Model.outlined][2]==click || Model.possibilities[Model.outlined][3]==click){
                       Model.nmodeflag=0;
                       Model.spaces[Model.outlined]=0;
                       Model.spaces[click]=Model.turn;
                       Model.outlined=100;
                       int line=Model.CheckLine();
                       if(line==1){
                           Model.PickppKukMode=true;
                           Model.NormalMode=false;
                           Model.PutKukriMode=false;
                           Model.m8="Congratulations Now Player "+Model.turn+" can Pick one Kukri of  Player "+(Model.turn==1?2:1)+"\n";
                           Model.m9=" Select Any One Opponent Kukri";
                           //m3="";
                       }
                       else{
                           if(Model.turn==1){
                            Model.m7="Player 2 Kukris left-->"+Model.p2KukLeft;
                            Model.m4="Player 1 Kukris left-->"+Model.p1KukLeft;
                            if(Model.p2KukLeft<=0){
                                Model.m10="Player 1 wins";
                            }
                            }
                            else{
                            Model.m4="Player 1 Kukris left-->"+Model.p1KukLeft;
                            Model.m7="Player 2 Kukris left-->"+Model.p2KukLeft;
                            if(Model.p1KukLeft<=0){
                                Model.m10="Player 2 wins";
                            }
                            }
                       Model.changeTurn();
                       }
                    }
                    else{
                        JOptionPane.showMessageDialog(null, "Sorry That Kukri cannot be moved there");
                    }

                }
                else{
                     JOptionPane.showMessageDialog(null, "Sorry The Space is not empty");
                }

            }  // if user clicks at wrong places
             else{
                     JOptionPane.showMessageDialog(null, "Sorry U clicked at Wrong Place");
             }


        } // else ends
        repaint();
    }

} // end of mouse clicked event



public void mouseEntered(MouseEvent me) {
// save coordinates


}
// Handle mouse exited.
public void mouseExited(MouseEvent me) {
// save coordinates

}
// Handle button pressed.

public void mousePressed(MouseEvent me) {

}
// Handle button released.
public void mouseReleased(MouseEvent me) {
// save coordinates

}
// Handle mouse dragged.
public void mouseDragged(MouseEvent me) {
// save coordinates
Model.mouseX = me.getX();
Model.mouseY = me.getY();
//Model.showStatus("Dragging mouse at " + mouseX + ", " + mouseY);
}
// Handle mouse moved.
public void mouseMoved(MouseEvent me) {
// show status
showStatus("Moving mouse at " + me.getX() + ", " + me.getY());
}
// Display msg in applet window.
public void paint(Graphics g) {
g.setColor(Color.red);

g.drawRect(300, 100, 800, 600);

g.drawRect(400, 200, 600, 400);

g.drawRect(500, 300, 400, 200);

g.drawLine(700, 100, 700, 300);
g.drawLine(300, 400, 500, 400);

g.drawLine(700, 500, 700, 700);
g.drawLine(900, 400, 1100, 400);


for(int i=0;i<24;i++){
    g.drawRect(Model.RectCordinates[i][0], Model.RectCordinates[i][1], Model.RectCordinates[i][2], Model.RectCordinates[i][3]);
}

for(int i=0;i<24;i++){
        Polygon p=new Polygon();
        p.addPoint(Model.RectCordinates[i][0],Model.RectCordinates[i][1]);
        p.addPoint(Model.RectCordinates[i][0]+50,Model.RectCordinates[i][1]);
        p.addPoint(Model.RectCordinates[i][0]+50,Model.RectCordinates[i][1]+50);
        p.addPoint(Model.RectCordinates[i][0],Model.RectCordinates[i][1]+50);
    if(Model.spaces[i]==0){

    }
    else if(Model.spaces[i]==1){
        g.setColor(Color.GREEN);
        g.fillRect(Model.RectCordinates[i][0],Model.RectCordinates[i][1],Model.RectCordinates[i][2], Model.RectCordinates[i][3]);
        g.setColor(Color.red);


    }
    else{
        g.setColor(Color.MAGENTA);
        g.fillRect(Model.RectCordinates[i][0],Model.RectCordinates[i][1],Model.RectCordinates[i][2], Model.RectCordinates[i][3]);
        g.setColor(Color.red);


    }
        if(Model.outlined!=100){
        g.setColor(Color.YELLOW);
        g.drawRect(Model.RectCordinates[Model.outlined][0]-2,Model.RectCordinates[Model.outlined][1]-2,Model.RectCordinates[Model.outlined][2], Model.RectCordinates[Model.outlined][3]);
        }
}
g.setColor(Color.YELLOW);
g.drawString(Model.m1, 10, 50);
g.setColor(Color.ORANGE);
g.drawString(Model.m2, 10, 70);
g.setColor(Color.CYAN);
g.drawString(Model.m3, 10, 90);
g.drawString(Model.m4, 10, 110);
g.setColor(Color.ORANGE);
g.drawString(Model.m5, 10, 130);
g.setColor(Color.CYAN);
g.drawString(Model.m6, 10, 150);
g.drawString(Model.m7, 10, 170);
g.setColor(Color.lightGray);
g.drawString(Model.m8, 10, 190);
g.drawString(Model.m9, 10, 210);
g.setColor(Color.white);
g.drawString(Model.m10, 10, 230);

}

}
